﻿ 
using System.Collections.Generic; 

namespace NTTDATA.BEENChina.ResultCertification.Util
{
    public class SplitList
    {
        /// <summary>
        /// 将一个List分割成指定段数
        /// </summary>
        /// <typeparam name="T">List中储存的实例类型</typeparam>
        /// <param name="sourceList">要进行分段的List</param>
        /// <param name="splitCount">要分割成的段数</param>
        /// <returns>分割结果集</returns>
        public static List<List<T>> Split<T>(List<T> sourceList, int splitCount)
        {
            var result = new List<List<T>>();
            int splitSize = sourceList.Count / splitCount; //长度
            int remainder = splitSize > 0 ? sourceList.Count % splitCount :
sourceList.Count; //余数
            for (int i = 0; i < splitCount; i++)
            {
                result.Add(sourceList.GetRange(i * splitSize, splitSize));
            }
            for (int i = 0; i < remainder; i++)
            {
                result[i].Add(sourceList[splitSize * splitCount + i]);
            }
            return result;
        }
    }
}
